package cn.gjxblog.aop.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInterceptor implements InvocationHandler {


    private UserService userService;

    public MyInterceptor(UserService object) {
        this.userService = object;
    }

    /**
     *
     * @param proxy  代表产生的代理类
     * @param method  要执行的方法
     * @param args 方法的允许参数
     * @throws Throwable
     * public final class $Proxy0 extends Proxy implements UserService
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(proxy instanceof UserService);// 接口实现
        System.out.println(proxy instanceof Proxy);// 子类继承
        System.out.println(Proxy.isProxyClass(proxy.getClass()));//是否是jdk代理类
        System.out.println(method);
        System.out.println(args);
        System.out.println(" Before method :" + method.getName() + " is invoked!");
        Object invoke = method.invoke(userService, args);
        System.out.println(" After method :" + method.getName() + " is invoked!");
        return invoke;
    }
}
